package com.qf.controller;

import com.qf.entity.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrdersService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrdersServiceImpl;
import com.qf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author:YuhuaZhang
 * @Description
 * @Date:Created in 11:51 2021/11/6
 * @Modified By:
 */
@WebServlet("/order")
public class OrdersController extends BaseServlet {

    public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {


        //1.获取请求参数
        String uid = request.getParameter("uid");

        //2.调用业务逻辑（地址，购物车）
        AddressService addressService=new AddressServiceImpl();
        List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));

        CartService cartService=new CartServiceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));

        //3.放入共享域即可
        request.setAttribute("addressList",addressList);
        request.setAttribute("cartList",cartList);

        return Constants.FORWARD+"/order.jsp";
    }

    public String create(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取请求参数
        String aid=request.getParameter("aid");
        String uid=request.getParameter("uid");
        String sum=request.getParameter("sum");
        //2.调用业务逻辑生成订单
        OrdersService ordersService=new OrdersServiceImpl();
        ordersService.createOrder(aid,uid,sum);

        //3.转发到订单展示的方法

        return Constants.FORWARD+"order?method=show";
    }


    public String show(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            session.setAttribute("msg","登录后才能查看订单！");
            return Constants.REDIRECT+"/login.jsp";
        }

        OrdersService ordersService=new OrdersServiceImpl();
        List<Orders> ordersList=ordersService.findOrdersByUid(user.getUid());

        request.setAttribute("ordersList",ordersList);
        return Constants.FORWARD+"/orderList.jsp";
    }

    public String detail(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        String oid = request.getParameter("oid");

        OrdersService ordersService=new OrdersServiceImpl();
        Orders order=ordersService.findOrdersByOid(oid);

        request.setAttribute("order",order);
        return Constants.FORWARD+"/orderDetail.jsp";
    }

}
